package com.archives.blog.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.EnumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.ui.ModelMap;

import com.archives.blog.common.domain.entity.Article;
import com.archives.blog.common.domain.entity.ArticleChannel;
import com.archives.blog.common.domain.pojo.request.ArticleQueryRequest;
import com.archives.blog.manager.ArticleChannelManager;
import com.archives.blog.manager.ArticleManager;
import com.archives.blog.web.routers.Routers;

public class BaseSupportController {
	
	@Autowired
	private ArticleChannelManager articleChannelManager;

	@Autowired
	private ArticleManager articleManager;

	public void router(ModelMap model, String templateName) {
		Routers router = EnumUtils.getEnum(Routers.class, templateName);
		if (router == null) {
			router = Routers.index;
		}
		List<ArticleChannel> channels = new ArrayList<ArticleChannel>();
		channels = articleChannelManager.getAll();
		model.addAttribute("channels", channels);
		ArticleQueryRequest res = new ArticleQueryRequest();
		res.setPageIndex(0);
		res.setPageSize(5);
		Page<Article> page = articleManager.getPage(res);
		if (page != null) {
			model.addAttribute("new_articles", page.getContent());
		}
		model.addAttribute("router_view", router);
	}
}
